import { Http, Jsonp, Request, RequestOptions, RequestOptionsArgs, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { MyResponse } from "./MyResponse"
import { MyHttpService } from "./MyHttpService";
import {userService} from "../../user/share/user.service";
/**
 * 自定义http请求服务类
 */
@Injectable()
export class MyHttpServiceImp implements MyHttpService {
    constructor(private http: Http, private jsonp: Jsonp,private UserService:userService) {

    }
    get<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.http.get(url, { search: this.buildSearchParams(params) }).toPromise().then(this.extractData).catch(this.handleError);
    }

    post<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.http.post(url, this.buildSearchParams(params)).toPromise().then(this.extractData).catch(this.handleError);
    };
    put<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.http.put(url, this.buildSearchParams(params)).toPromise().then(this.extractData).catch(this.handleError);
    };
    delete<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.http.delete(url, { search: this.buildSearchParams(params) }).toPromise().then(this.extractData).catch(this.handleError);
    };
    jsonpGet<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.jsonp.get(url, { search: this.buildSearchParams(params) }).toPromise().then(this.extractData).catch(this.handleError);

    };
    jsonpPost<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.jsonp.post(url, this.buildSearchParams(params)).toPromise().then(this.extractData).catch(this.handleError);

    };
    jsonpPut<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.jsonp.put(url, this.buildSearchParams(params)).toPromise().then(this.extractData).catch(this.handleError);
    };
    jsonpDelete<T>(url: string, params?: {}): Promise<MyResponse<T>> {
        return this.jsonp.delete(url, { search: this.buildSearchParams(params) }).toPromise().then(this.extractData).catch(this.handleError);
    };
    private buildSearchParams(obj): URLSearchParams {
        let params = new URLSearchParams();
        for (let key in obj) {
            let val = obj[key];
            params.set(key, val);
        }
        return params;
    }
    private extractData(res: Response) {
        let body = res.json();
        return body.data || {};
    }
    private handleError(error: Response | any) {
        let errMsg: string;
        if (error instanceof Response) {
            const body = error.json() || '';
            const err = body.error || JSON.stringify(body);
            errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
            errMsg = error.message ? error.message : error.toString();
        }
       // console.error(errMsg);
        return Promise.reject(errMsg);
    }
    //进行登录和权限校验
    private getAuth():boolean{
            if(this.UserService.isLogin){
                return true;
            }else{
                return false;
            }
    }
}